from typing import Optional, Dict, Any
from app.models.notes_model import NoteResult
from app.models.audio_model import AudioDownloadResult
from app.models.transcriber_model import TranscriptResult
from app.db.task_dao import TaskDAO
from app.utils.logger import get_logger

logger = get_logger(__name__)

class NoteService:
    def __init__(self):
        self.task_dao = TaskDAO()

    def get_note(self, task_id: str) -> Optional[NoteResult]:
        """获取笔记内容"""
        try:
            task_data = self.task_dao.get_task(task_id)
            if not task_data:
                return None
            
            # 将字典转换为对应的数据类型
            audio_meta = task_data.get("audio_meta", {})
            if isinstance(audio_meta, dict):
                audio_meta = AudioDownloadResult(
                    file_path=audio_meta.get("file_path", ""),
                    video_id=audio_meta.get("video_id", ""),
                    title=audio_meta.get("title", ""),
                    duration=audio_meta.get("duration", 0),
                    cover_url=audio_meta.get("cover_url", ""),
                    platform=audio_meta.get("platform", "unknown"),
                    raw_info=audio_meta.get("raw_info", {}),
                    video_path=audio_meta.get("video_path")
                )
            
            transcript = task_data.get("transcript", {})
            if isinstance(transcript, dict):
                transcript = TranscriptResult(
                    full_text=transcript.get("full_text", ""),
                    language=transcript.get("language", ""),
                    segments=transcript.get("segments", []),
                    raw=transcript.get("raw", {})
                )
            
            return NoteResult(
                markdown=task_data.get("markdown", ""),
                transcript=transcript,
                audio_meta=audio_meta
            )
        except Exception as e:
            logger.error(f"获取笔记失败: {e}")
            return None 